// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_START, this.startHandler, this)
    }

    start() {

    }

    startHandler(){
        const rigid: cc.RigidBody = this.node.getComponent(cc.RigidBody)
        const impulse : cc.Vec2 = cc.v2(0, 2000) // 声明一个冲量
        let pt: cc.Vec2 = cc.v2(0, 0); // 作用点
        pt = this.node.convertToWorldSpaceAR(pt); // 需要将作用点转换成世界坐标
        rigid.applyLinearImpulse(impulse, pt, true); // 添加一个线性冲量
    }

    // update (dt) {}

    // 需要在编辑器中开启 Enable Contact Listener 回调函数才能执行  谁在层级管理器中的层级高就先执行谁
    onBeginContact (contact: cc.PhysicsContact, self: cc.Collider, other: cc.Collider) {
        const worldManifold = contact.getWorldManifold(); // 世界坐标系信息
        const points = worldManifold.points; // 碰撞点数组
        // const normal = worldManifold.normal; // 碰撞点上的法向量
        cc.log(self.node.name, points)
    }

    // 只在两个碰撞体结束接触时被调用
    onEndContact (contact, selfCollider, otherCollider) {
        cc.log('只在两个碰撞体结束接触时被调用')
    }

    // // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve (contact, selfCollider, otherCollider) {
        cc.log('每次将要处理碰撞体接触逻辑时被调用')
    }

    // // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve (contact, selfCollider, otherCollider) {
        cc.log('每次处理完碰撞体接触逻辑时被调用')
    }
}
